package com.lykj.llw.box.ui.activity.agent

import android.graphics.Typeface
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.ColorUtils
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.lykj.lib_base.adapter.viewpager.FragmentSPAdapter
import com.lykj.lib_base.base.BaseActivity
import com.lykj.lib_base.internal.extfun.clickWithTrigger
import com.lykj.llw.box.R
import com.lykj.llw.box.databinding.ActAgentVoucherBinding
import com.lykj.llw.box.ui.fragment.ReUseAgentVoucherFragment

/**
 * ================================================
 * 代金券页面
 * ================================================
 *
 * @author gosha
 * @date 2023/10/30-16:54
 */
class AgentVoucherActivity : BaseActivity() {
    lateinit var binding: ActAgentVoucherBinding

    private val mFragments = ArrayList<Fragment>()
    private val mBuyFragment = ReUseAgentVoucherFragment()
    private val mVoucherFragment = ReUseAgentVoucherFragment()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActAgentVoucherBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initUI()
        initData()
        setListener()
    }

    override fun onResume() {
        super.onResume()

        ImmersionBar.with(this)
            .statusBarDarkFont(true) //状态栏字体是深色，不写默认为亮色
            .navigationBarDarkIcon(true)
            .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
            .navigationBarEnable(false)
            .init()
    }

    override fun initUI() {
        binding.titleBar.tvTitle.text = "代金券"
        binding.titleBar.tvRightOption.visibility = View.GONE

        val mBuyBundle = Bundle()
        mBuyBundle.putInt("type", 0)

        val mVoucherBundle = Bundle()
        mVoucherBundle.putInt("type", 1)

        mBuyFragment.arguments = mBuyBundle
        mVoucherFragment.arguments = mVoucherBundle

        mFragments.add(mBuyFragment)
        mFragments.add(mVoucherFragment)

        //设置viewpager
        val adapter = FragmentSPAdapter(supportFragmentManager, mFragments)
        binding.vpVoucher.offscreenPageLimit = 1
        binding.vpVoucher.adapter = adapter
        switchNav(0)
    }

    //重置底部导航栏状态
    private fun restNav() {
        binding.tvBuyDesc.setTextColor(ColorUtils.getColor(R.color.grey_FF999999))
        binding.tvVoucherDesc.setTextColor(ColorUtils.getColor(R.color.grey_FF999999))

        binding.vLineBuy.visibility = View.GONE
        binding.vLineVoucher.visibility = View.GONE
    }

    //切换到导航栏到指定页面
    private fun switchNav(position: Int) {
        restNav()
        when (position) {
            0 -> {
                binding.tvBuyDesc.setTextColor(ColorUtils.getColor(R.color.black_FF2C2C2C))
                binding.vLineBuy.visibility = View.VISIBLE
            }

            1 -> {
                binding.tvVoucherDesc.setTextColor(ColorUtils.getColor(R.color.black_FF2C2C2C))
                binding.vLineVoucher.visibility = View.VISIBLE
            }
        }
        binding.vpVoucher.setCurrentItem(position, false)
    }

    override fun initData() {
    }

    override fun setListener() {
        binding.titleBar.ivBack.clickWithTrigger {
            finish()
        }

        binding.lltBuy.clickWithTrigger(200) {
            switchNav(0)
        }

        binding.lltVoucher.clickWithTrigger(200) {
            switchNav(1)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
    }
}